package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.aliyun.oss.OSSClient;
import com.example.demo.po.*;
import com.example.demo.service.UserManagerService;
import com.example.demo.service.WxUserInfoService;
import com.example.demo.util.UploadALiYunOOS;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userManager")
public class UserManagerController {
    @Autowired
    private UserManagerService userService;
    @Autowired
    WxUserInfoService wxUserInfoService;


    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

    @RequestMapping("/getUser/{username}")
    public String GetUser(@PathVariable String username) {
        Map<String, UserManage> map = new HashMap<>();
        UserManage user = userService.getByUserName(username);
        map.put("user",user);
        return map.toString();
    }


    @RequestMapping("/userInfo")
    public ModelAndView updateInit(HttpServletRequest request, HttpServletResponse response){
        ModelAndView view = new ModelAndView();
        HttpSession session = request.getSession();
        UserManage user = (UserManage) session.getAttribute("session_user");
        view.setViewName("user/user_update");
        view.addObject("user",user);
        return  view;
    }


    @RequestMapping(value = "sOuUser" ,method = RequestMethod.POST)
    public String saveOrUpdate(@RequestParam Map<String,String> map){
        try{
            UserManage userManage =new UserManage();
            if(map.containsKey("id"))
                userManage.setId(
                        Integer.parseInt(map.get("id")));
            if(map.containsKey("userName"))
                userManage.setUserName(map.get("userName"));
            if(map.containsKey("userPwd"))
                userManage.setUserPwd(map.get("userPwd"));
            if(map.containsKey("realName"))
                userManage.setRealName(map.get("realName"));
            boolean results =userService.saveOrUpdate(userManage);
            if(results)
                return "success";
            else
                return "false";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "success";
    }

    @RequestMapping(value = "tset" ,method = RequestMethod.POST)
    public String test(@RequestBody TestPo testPo){
        System.out.println(testPo.getRealname());
        return "true";
    }

    @RequestMapping(value = "/wxUserlist")
    public ModelAndView wxUserlist(@ModelAttribute WxUserInfo wxUserInfo,
                             HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        PageRequest page = new PageRequest();
        page.setPageNum(wxUserInfo.getPageNum());
        page.setPageSize(wxUserInfo.getPageSize());
        PageResult resilt = wxUserInfoService.findByPage(wxUserInfo,page);
        view.setViewName("/user/wx_userinfo_list");
        view.addObject("result", JSON.toJSON(resilt));
        return  view;
    }


}
